<?php
namespace jy_msgorder;
defined('BY_JYA') or exit('error');
/*
	商品
*/
class goods extends \plugin{
	public function get_comments($workerid,$limit=false,&$total,$type='all',$page=0,$merchid=0){
		$params=array('workerid'=>$workerid,'show'=>1);
		$parameter['orderby']=array('star desc','id desc');
		if($type=='percent'){
			$params['star']=5;
		}
		if($type=='normal_percent'){
			$params['star']=array(2,3,4);
		}
		if($type=='bad_percent'){
			$params['star']=array(0,1);
		}
		if($type=='pic'){
			$params['images !=']='';
		}
		if($type=='new'){
			$parameter['orderby']=array('createtime desc','star desc');
		}
		$parameter['params']=$params;
		$pindex = max(1, intval($page));
		$psize = $limit?:5;
		$parameter['limit']=array($pindex,$psize);
		$list=$this->pdo_getlist('comment',$parameter,$total);
		if($list){
			foreach($list as &$item){
				$item['member']=model('core/member')->detail($item['mid'],array('nickname','avatar'));
				$item['member']['nickname']=$item['member']['nickname']?:'匿名用户';
				$item['images']=$item['images']?json_decode($item['images'],true):array();
				$item['workertitle']=pdo_getcolumn('jy_msgorder_worker',array('id'=>$item['workerid'],'uniacid' => $_SESSION['uniacid']),'title');
				$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			}
		}
		return $list;
	}
	public function count_comments($workerid){
		$percent = 100;
		$count = array(
			'all'=>$this->pdo_count('comment',array('workerid'=>$workerid)),
			'good'=>$this->pdo_count('comment',array('workerid'=>$workerid,'star'=>5)),
			'normal'=>$this->pdo_count('comment',array('workerid'=>$workerid,'star between'=>array(2,4))),
			'bad'=>$this->pdo_count('comment',array('workerid'=>$workerid,'star'=>array(0,1))),
			'pic'=>$this->pdo_count('comment',array('workerid'=>$workerid,'images !='=>'')),
		);
		if(0 < $count['all']){
			$percent = intval(($count['good'] / $count['all']) * 100);
			$normal_percent= intval(($count['normal'] / $count['all']) * 100);
			$bad_percent= intval(($count['bad'] / $count['all']) * 100);
			$pic= intval(($count['pic'] / $count['all']) * 100);
		}
		return array(
			"count" => $count,
			'percent' => $percent,
			'normal_percent'=>$normal_percent,
			'bad_percent'=>$bad_percent,
			'pic'=>$pic
		);
	}
}